package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.host.dao.HostWithdrawDao;
import ysq.xyg.host.model.po.HostWithdraw;
import ysq.xyg.host.util.HostBaseContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostWithdrawVo;
import ysq.xyg.host.service.HostWithdrawService;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import javax.persistence.Transient;
import java.util.Date;
import java.util.List;


/**
 * 房产模块-提现记录service实现
 *
 * @author shengQiang yu
 * @date 2019-7-7 20:49:44
 */
@Service("hostWithdrawService")
public class HostWithdrawServiceImpl extends BaseServiceImpl<HostWithdraw> implements HostWithdrawService {
    @Resource
    private WebMemberService webMemberService;
    @Resource
    private HostWithdrawDao hostWithdrawDao;

    private HostWithdraw hostWithdraw;

    @Autowired
    public void setBaseDao(HostWithdrawDao hostWithdrawDao) {
        this.baseDao = hostWithdrawDao;
    }

    /**
     * 查询集合
     *
     * @param hostWithdrawVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:08:52
     */
    public List<HostWithdraw> listLeft(HostWithdrawVo hostWithdrawVo) {
        List<HostWithdraw> list = hostWithdrawDao.listLeft(hostWithdrawVo);
        if(list!=null&&list.size()>0){
            for (HostWithdraw hostWithdraw:list){
                if(StrUtil.isNotEmpty(hostWithdraw.getNickName())){
                    hostWithdraw.setNickName(BaseUtil.decodeBase64(hostWithdraw.getNickName()));
                }
            }
        }
        return list;
    }
    /**
     * 分页查询
     *
     * @param hostWithdrawVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-7 20:49:44
     */
    public ReturnEntity page(HostWithdrawVo hostWithdrawVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostWithdraw> list = listLeft(hostWithdrawVo);
        PageInfo<HostWithdraw> page=new PageInfo<HostWithdraw>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostWithdrawVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-7 20:49:44
     */
    @Transient
    public ReturnEntity addOrUpdate(HostWithdrawVo hostWithdrawVo, SysUser currentUser) {
        hostWithdraw=selectByPrimaryKey(hostWithdrawVo.getId());
        if(ObjectUtil.isNull(hostWithdraw)){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        hostWithdraw.setUpdateTime(new Date());
        hostWithdraw.setUpdateUser(currentUser.getId());
        hostWithdraw.setWithdrowStatus(hostWithdrawVo.getWithdrowStatus());
        hostWithdraw.setResult(hostWithdrawVo.getResult());
        int num=updateByPrimaryKey(hostWithdraw);
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }else{
            if(HostBaseContent.HOST_WITHDRAW_STATUS_TXSB.equals(hostWithdraw.getWithdrowStatus())){
                //审核失败
                WebMember webMember=webMemberService.selectByPrimaryKey(hostWithdraw.getMemberId());
                webMember.setMoney(webMember.getMoney()+hostWithdraw.getMoney());
                webMemberService.updateByPrimaryKey(webMember);
            }
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-7 20:49:44
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostWithdraw hostWithdraw=selectByPrimaryKey(id);
        if(hostWithdraw!=null){
            hostWithdraw.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostWithdraw.setUpdateTime(new Date());
            hostWithdraw.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostWithdraw);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostWithdrawVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-7 20:49:44
     */
    public List<HostWithdraw> acceptList(HostWithdrawVo hostWithdrawVo){
        Example example = new Example(HostWithdraw.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostWithdrawVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostWithdrawVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return hostWithdrawDao.selectByExample(example);
    }

    @Override
    public Integer selectSumMoney(String memberId, String withdrowStatus) {
        return hostWithdrawDao.selectSumMoney(memberId, withdrowStatus);
    }
}
